<template>
  <div id="main-app">
    <RouterView />
    <div class="fixedVx" @click="popVx">
      <div><img src="../src/assets/vx.svg" alt="" /></div>
    </div>
    <van-popup v-model:show="vxShow" round :duration="0" :close-on-click-overlay="false">
      <div class="vx-detail">
        <div class="vx-close" @click="closePopvx">
          <van-icon name="cross" :size="16" />
        </div>
        <van-icon name="passed" size="48" color="#1aad19" />
        <div class="vx-tip">微信号复制成功</div>
        <div>
          <span>微信号(WeChat):</span><span class="vx-num">{{ vxCode }}</span>
        </div>
        <div>
          <van-image class="vx-img" :src="vxUrl">
            <template v-slot:loading>
              <van-loading type="spinner" size="36" />
            </template>
          </van-image>
          <!-- <img  :src="vxUrl" alt=""> -->
        </div>
        <div>
          <van-button type="success" size="small" @click="openVx">我知道了</van-button>
        </div>
      </div>
    </van-popup>
    <van-popup v-model:show="showProxy" :close-on-click-overlay="false" round :style="{ padding: '64px' }">
      {{ proxy }}
    </van-popup>
  </div>
</template>
<style>
@media (max-width: 670px) {
  ::-webkit-scrollbar {
    display: none;
  }
}

#main-app {
  /* overflow: hidden; */
  height: 100%;
}

::-webkit-scrollbar {
  width: 8px;
  height: 10px;
}

::-webkit-scrollbar-track {
  width: 6px;
  background: rgb(240, 240, 240);
  -webkit-border-radius: 2em;
  -moz-border-radius: 2em;
  /* border-radius: 2em; */
}

::-webkit-scrollbar-thumb {
  background-color: rgba(144, 147, 153, 0.5);
  background-clip: padding-box;
  min-height: 28px;
  -webkit-border-radius: 2em;
  -moz-border-radius: 2em;
  border-radius: 2em;
  transition: background-color 0.3s;
  cursor: pointer;
}

::-webkit-scrollbar-thumb:hover {
  background-color: rgba(144, 147, 153, 0.3);
}
</style>
<script setup>
import { generateProxy, proxy_req } from "@/utils/utils";
import { showFailToast } from "vant";
import { reactive, ref } from "vue";
import { useRouter, RouterView } from "vue-router";
const vxShow = ref(false);
const showProxy = ref(false);
const proxy = ref("");
const vxUrl = ref("");
const vxCode = ref("");
const router = useRouter();
const state = reactive({
  transitionName: "slide-left",
});
const getVxImgUrl = async () => {
  try {
    const response = await fetch("/config", {
      method: "GET",
      headers: {
        "Content-Type": "application/json",
      },
      referrerPolicy: "no-referrer",
    });
    const res = await response.json();
    const vxCode = (await JSON.parse(res.data.file.data).vxCode) ?? "AFMY458";
    const vxUrl = (await JSON.parse(res.data.file.data).vxImgUrl) ?? "";
    const vx = {
      code: vxCode,
      url: vxUrl,
    };
    // showToast(JSON.stringify(vx))
    // console.log(vx);
    return vx;
  } catch (error) {
    console.log(error);
    return {};
  }
};
const closePopvx = () => {
  vxShow.value = false;
};
const openVx = () => {
  vxShow.value = false;
  window.location.href = "weixin://";
};
const popVx = async () => {
  vxShow.value = true;
  const { code, url } = await getVxImgUrl();
  const val = code.value ?? "AFMY458";
  if (window.navigator.clipboard && window.isSecureContext) {
    navigator.clipboard.writeText(val).then(
      () => {
        console.log("粘贴文本成功");
      },
      () => {
        console.log("clipboard write failed");
      }
    );
  } else {
    const textArea = document.createElement("textarea");
    textArea.value = val;
    // 使text area不在viewport，同时设置不可见
    document.body.appendChild(textArea);
    textArea.focus();
    textArea.select();
    new Promise((res, rej) => {
      // 执行复制命令并移除文本框
      document.execCommand("copy") ? res() : rej();
      textArea.remove();
    });
  }
  vxCode.value = code ?? "";
  vxUrl.value = url ?? "";
};
const setProxy = () => {
  const mi = import.meta.env.VITE_MIN_RANGE;
  const ma = import.meta.env.VITE_MAX_RANGE;
  const p = import.meta.env.VITE_MIN_DATE;
  const go = generateProxy(mi, ma, p);
  showProxy.value = go > 0.5;
  proxy.value = decodeURIComponent(proxy_req);
};
router.beforeEach((to, from) => {
  if (to.meta.index > from.meta.index) {
    state.transitionName = "slide-left"; // 向左滑动
  } else if (to.meta.index < from.meta.index) {
    // 由次级到主级
    state.transitionName = "slide-right";
  } else {
    state.transitionName = ""; // 同级无过渡效果
  }
});
// setProxy();
</script>
<style lang="less">
.vx-detail {
  position: relative;
  padding: 0.5rem 0 0.3rem 0;
  min-width: 240px;
  text-align: center;
  display: flex;
  flex-direction: column;

  .vx-close {
    position: absolute;
    right: 0.25rem;
    top: 0.25rem;
    cursor: pointer;
  }

  .vx-tip {
    margin: 5px;
    font-weight: 600;
    font-size: 16px;
    color: #1aad19;
  }

  .vx-num {
    color: rgb(255, 0, 0);
    font-weight: 600;
    font-size: 12px;
    margin-left: 10px;
  }

  .vx-img {
    height: 240px;
  }
}

.fixedVx {
  cursor: pointer;
  position: fixed;
  bottom: 3rem;
  right: 1rem;
  width: 35px;
  height: 35px;
  border-radius: 50%;
  background: #ffffffcf;
  z-index: 999;
  display: flex;
  justify-content: center;
  align-items: center;
  color: #fff;
  transition: all 0.1s;

  img {
    box-shadow: 0 0 10px #00000064;
    border-radius: 50%;
    height: 40px;
    width: 40px;
  }
}

html,
body {
  height: 100%;
  overflow-x: hidden;
}

#app {
  height: 100%;
  font-family: "Avenir", Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  color: #2c3e50;
}

.router-view {
  width: 100%;
  height: auto;
  position: absolute;
  top: 0;
  bottom: 0;
  margin: 0 auto;
  -webkit-overflow-scrolling: touch;
}

.slide-right-enter-active,
.slide-right-leave-active,
.slide-left-enter-active,
.slide-left-leave-active {
  height: 100%;
  will-change: transform;
  transition: all 500ms;
  position: absolute;
  backface-visibility: hidden;
}

.slide-right-enter {
  opacity: 0;
  transform: translate3d(-100%, 0, 0);
}

.slide-right-leave-active {
  opacity: 0;
  transform: translate3d(100%, 0, 0);
}

.slide-left-enter {
  opacity: 0;
  transform: translate3d(100%, 0, 0);
}

.slide-left-leave-active {
  opacity: 0;
  transform: translate3d(-100%, 0, 0);
}

.van-badge--fixed {
  z-index: 1000;
}
</style>
